package week4th.jsjf;

public class LinkedUnorderedList<T> extends LinkedList<T>
         implements UnorderedListADT<T>
{

    public LinkedUnorderedList()
    {
        super();
    }

    public void addToFront(T element)
    {
        LinearNode node = new LinearNode(element);
        if (head == null) {
            head = node;
            tail = node;
        }else {
            node.setNext(head);
            head = node;
        }
        count++;
    }

    public void addToRear(T element)
    {
        LinearNode current = head;
        LinearNode node = new LinearNode(element);
        while (current.getNext() != null) {
            current = current.getNext();
        }
        node.setNext(node);
        count++;
    }

    public void addAfter(T element, T target)
    {
        LinearNode<T> node = new LinearNode<>(element);
        LinearNode<T> current = head;
        LinearNode<T> temp = current.getNext();
        for (int i = 0;i < count - 1;i++){
            if (target.equals(current.getElement())) {
                current.setNext(node);
                node.setNext(temp);
                count++;
                return;
            }
            current = temp;
            temp = temp.getNext();
        }
    }	
}
